package com.zhongying.jf.huifuUtils;

import chinapnr.SecureLink;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.util.Map;

/**
 * @author fengzhengjun
 * @Description
 * @Email fengzhengjun@chinazyjr.com
 * Created on 2017/8/30 13:30
 * Copyright (c) 2017 All Rights Reserved.
 */
public class HuifuSign {
    /**
     * 生成签名
     *
     * @param md5Flag
     * @param signType
     * @param inMap
     * @return
     * @throws Exception
     */
    public static String createSign(boolean md5Flag, String[] signType, Map<String, String> inMap, String shRecuMerId, String privateKeyPath) throws Exception {
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < signType.length; i++) {
            if (inMap.containsKey(signType[i])
                    && inMap.get(signType[i]) != null
                    && !"".equals(inMap.get(signType[i]))) {
                buffer.append(inMap.get(signType[i]));
            }
        }
        if (buffer.toString() == null || "".equals(buffer.toString())) {
            return "";
        }
        System.out.println("验签拼接："+buffer.toString());
        String signStr = !md5Flag ? buffer.toString() : createSignByMd5(buffer.toString());
        System.out.println("MD5加密："+signStr);
        SecureLink secureLink = new SecureLink();
        secureLink.SignMsg(shRecuMerId, privateKeyPath, signStr);
        String chkValue = secureLink.getChkValue();
        inMap.put("ChkValue", chkValue);
        return  chkValue;
    }

    /**
     * 校验签名
     *
     * @param sign
     * @param md5Flag
     * @param signType
     * @param outMap
     * @return true:校验成功/false:校验失败
     * @throws Exception
     */
    public static boolean verifySign(String sign, boolean md5Flag, String[] signType, Map<String, Object> outMap, String publicKeyPath) throws Exception {
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < signType.length; i++) {
            if (outMap.containsKey(signType[i])
                    && outMap.get(signType[i]) != null
                    && !"".equals(outMap.get(signType[i]))
                    && !"null".equals(outMap.get(signType[i]))) {
                buffer.append(outMap.get(signType[i]));
            }
        }
        if (buffer.toString() == null || "".equals(buffer.toString())) {
            return false;
        }
        System.out.print("验签校验拼接："+buffer.toString());
        String signStr = !md5Flag ? buffer.toString() : createSignByMd5(buffer.toString());
        System.out.print("验签校验MD5加密："+signStr);
        SecureLink secureLink = new SecureLink();
        int resultCode = secureLink.VeriSignMsg(publicKeyPath, signStr, sign);
        if (resultCode != 0) {
            return false;
        }
        return  true;
    }

    /**
     * 请求参数时转码
     *
     * @param param
     * @return
     * @throws Exception
     */
    public static String encoding(String param) throws UnsupportedEncodingException {
        return (param == null || "".equals(param)) ? "" : URLEncoder.encode(param, "UTF-8");
    }

    /**
     * 响应结果时反转码
     *
     * @param param
     * @return
     * @throws Exception
     */
    public static String decoding(String param) throws UnsupportedEncodingException {
        return (param == null || "".equals(param)) ? "" : URLDecoder.decode(param, "UTF-8");
    }

    /**
     * 生成签名
     *
     * @param strSrc
     * @return
     * @throws Exception
     */
    private static String createSignByMd5(String strSrc) throws Exception {
        StringBuffer buffer = new StringBuffer("");
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        byte[] outByte = md5.digest(strSrc.getBytes("UTF-8"));
        int i;
        for (int offset = 0; offset < outByte.length; offset++) {
            i = outByte[offset];
            if (i < 0)
                i += 256;
            if (i < 16)
                buffer.append("0");
            buffer.append(Integer.toHexString(i));
        }
        return buffer.toString();
    }
}
